package com.alg;

import java.util.*;

/**
 * @author: sun.hongliang
 * @create: 2019/12/31 17:55
 */
public class HuiSu {

    public static void main(String[] args) {
        int[] candidates = {1, 2, 3};
        int target = 7;
        HuiSu solution = new HuiSu();
        List<List<Integer>> combine = solution.combine(4, 2);
        System.out.println(combine);

        ThreadLocal threadLocal = new ThreadLocal();
        threadLocal.set("a");
        threadLocal.get();
    }

    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        if (candidates.length == 0) return Collections.emptyList();
        Arrays.sort(candidates);
        List<List<Integer>> res = new ArrayList<>();
        Stack<Integer> temp = new Stack<>();
        combination(candidates, res, 0, target, temp);
        return res;
    }

    private void combination(int[] candidates, List<List<Integer>> res, int start, int remain, Stack<Integer> temp) {
        if (remain == 0) {
            res.add(new ArrayList<>(temp));
            return;
        }
        for (int i = start; i < candidates.length && remain - candidates[i] >= 0; i++) {
            int newR = remain - candidates[i];
            temp.add(candidates[i]);
            combination(candidates, res, i + 1, newR, temp);
            temp.pop();
        }
    }

    public List<List<Integer>> permute(int[] nums) {
        if (nums == null || nums.length == 0) return Collections.emptyList();
        List<List<Integer>> result = new ArrayList<>();
        int[] vis = new int[nums.length];
        comb(nums, result, new Stack<>(), vis);
        return result;
    }

    private void comb(int[] nums, List<List<Integer>> result, Stack<Integer> stack, int[] vis) {
        if (stack.size() == nums.length) {
            result.add(new ArrayList<>(stack));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (vis[i] != 1) {
                stack.push(nums[i]);
                vis[i] = 1;
                comb(nums, result, stack, vis);
                stack.pop();
                vis[i] = 0;
            }
        }
    }


    public String getPermutation(int n, int k) {
        if (n < 1) return null;
        int[] nums = new int[n];
        for (int i = 0; i < nums.length; i++) {
            nums[i] = i + 1;
        }
        List<List<Integer>> res = new ArrayList<>();
        int[] vis = new int[nums.length];
        combPermutation(nums, res, new Stack<>(), k, vis);
        System.out.println(res);
        List<Integer> list = res.size() >= k ? res.get(k - 1) : new ArrayList<>();
        StringBuilder sb = new StringBuilder();
        for (Integer i : list) {
            sb.append(i);
        }
        return sb.toString();
    }

    public void combPermutation(int[] nums, List<List<Integer>> res, Stack<Integer> temp, int k, int[] vis) {
//        if (res.size() > k) {
//            return;
//        }
        if (temp.size() == nums.length) {
            res.add(new ArrayList<>(temp));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (vis[i] == 0) {
                temp.add(nums[i]);
                vis[i] = 1;
                combPermutation(nums, res, temp, k, vis);
                temp.pop();
                vis[i] = 0;
            }
        }
    }


    public List<List<Integer>> combine(int n, int k) {
        if (n < 1) return Collections.emptyList();
        int[] nums = new int[n];
        for (int i = 0; i < n; i++) {
            nums[i] = i + 1;
        }
        int[] vis = new int[n];
        List<List<Integer>> res = new ArrayList<>();
        combinecombine(nums, res, k, new Stack<>(), vis, 0);
        return res;
    }

    private void combinecombine(int[] nums, List<List<Integer>> res, int k, Stack<Integer> stack, int[] vis, int start) {
        if (stack.size() == k) {
            res.add(new ArrayList<>(stack));
            return;
        }
        for (int i = start; i < nums.length; i++) {
            if (vis[i] == 0) {
                stack.push(nums[i]);
                vis[i] = 1;
                combinecombine(nums, res, k, stack, vis, i + 1);
                stack.pop();
                vis[i] = 0;
            }
        }
    }


    public List<List<String>> partition(String s) {
        int len = s.length();
        List<List<String>> res = new ArrayList<>();
        if (len == 0) {
            return res;
        }
        Deque<String> stack = new ArrayDeque<>();
        backtracking(s, 0, len, stack, res);
        return res;
    }

    private void backtracking(String s, int start, int len, Deque<String> path, List<List<String>> res) {
        if (start == len) {
            res.add(new ArrayList<>(path));
            return;
        }
        for (int i = start; i < len; i++) {
            if (!checkPalindrome(s, start, i)) {
                continue;
            }
            path.addLast(s.substring(start, i + 1));
            backtracking(s, i + 1, len, path, res);
            path.removeLast();
        }
    }

    private boolean checkPalindrome(String str, int left, int right) {
        while (left < right) {
            if (str.charAt(left) != str.charAt(right)) {
                return false;
            }
            left++;
            right--;
        }
        return true;
    }
}
